<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'edit';
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		if($_RQ['ishuo']){
			$id=pdo_getcolumn('jy_worker_ewm_worker',array('merchid'=>$_SESSION['onemerchid'],'id'=>$id,'uniacid'=>$_SESSION['uniacid']),'workerid');
		}
		$item=pdo_get('jy_scrm_worker_gift',array('merchid'=>$_SESSION['onemerchid'],'workerid'=>$id,'uniacid'=>$_SESSION['uniacid']));
		if(!$item){
			$item=array('type'=>0,'workerid'=>$id);
		}else{
			$item['endtime'] =$item['endtime']?date('Y/m/d H:i',$item['endtime']):'';
			$item['reward'] =json_decode($item['reward'],true);
		}
		$item['msg']=urldecode($item['msg']);
	}else{
		exi('请选择员工','error');
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'edit'=>array(
			array('title'=>'[温馨提示]：加好友后，会自动赠送下面礼品，并回复【送礼文字回复】','type'=>'tip'),
			array('name'=>'msg','title'=>'送礼文字回复','type'=>'text_wx','help'=>'[客户名称]表示客户名称变量'),
			array('name'=>'endtime','title'=>'结束时间','type'=>'time'),
			array(
			'type'=>'array',
			'title'=>'送礼品设置',
			'name'=>'reward',
			'add_tip'=>'添加一个礼品',
				'list'=>array(
					array('type'=>'radio','title'=>'送礼类型','name'=>'type','help'=>'','options'=>'types'),
					array('type'=>'radio','title'=>'送币类型','name'=>'credit_type','help'=>'','options'=>'credit_types','show'=>array('type'=>0)),
					array('type'=>'unitstring','unit'=>'个','title'=>'赠送币个数','name'=>'credit_num','show'=>array('type'=>0)),
					array('type'=>'unitstring','unit'=>'张','title'=>'赠送币优惠券数','name'=>'coupon_num','show'=>array('type'=>1)),
					array('type'=>'unitstring','unit'=>'次','title'=>'赠送币优惠券数','name'=>'game_num','show'=>array('type'=>2)),
				),
				'details'=>array(
					array('type'=>'radio','title'=>'送礼类型','name'=>'type','help'=>'','options'=>'types'),
					array('type'=>'radio','title'=>'送币类型','name'=>'credit_type','help'=>'','options'=>'credit_types','show'=>array('type'=>0)),
					array('type'=>'unitstring','unit'=>'个','title'=>'赠送币个数','name'=>'credit_num','show'=>array('type'=>0)),
					array('name'=>'couponid','title'=>'选择送优惠券','type'=>'select','help'=>'','options'=>'coupons','show'=>array('type'=>1)),
					array('type'=>'unitstring','unit'=>'张','title'=>'赠送币优惠券数','name'=>'coupon_num','show'=>array('type'=>1)),
					array('name'=>'jy_bigwheelid','title'=>'选择大转盘活动','type'=>'select','help'=>'','options'=>'games','show'=>array('type'=>2)),
					array('type'=>'unitstring','unit'=>'次','title'=>'赠送币优惠券数','name'=>'game_num','show'=>array('type'=>2)),
				)
			),
			array('type'=>'switch','title'=>'状态','name'=>'status','help'=>''),
		)
	);
	$return['coupons']=pdo_getall('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']),array('title','id'),'id');
	$return['games']=pdo_getall('jy_weishop_game',array('status'=>1,'plugin_name'=>'jy_bigwheel','uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']),array('title','id'),'id');
	$return['types']=array('送币','送优惠券','送抽奖机会');
	$return['credit_types']=credit_types();
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'msg'=>urlEncode($postdata['msg']),
		'endtime'=>strtotime($postdata['endtime']),
		'reward'=>ijson_encode($postdata['reward']),
		'status'=>intval($postdata['status']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_scrm_worker_gift',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$_SESSION['onemerchid'];
		$data['workerid']=$postdata['workerid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_scrm_worker_gift',$data);
	}
	exi('操作成功！','','goback');
}